package com.cobra.exception;

import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;


@ControllerAdvice
public class MyExceptionHandler {
    @ExceptionHandler(value =Exception.class)
    @ResponseBody
    public String exceptionHandler(Exception e){
        JSONObject globalException = new JSONObject();
        globalException.put("message",e.getMessage());
        globalException.put("type","global");
        return globalException.toJSONString();
    }

    @ExceptionHandler(value =NotEnoughMoneyException.class)
    @ResponseBody
    public String notEnoughMoneyExceptionHandler(NotEnoughMoneyException e){
        JSONObject globalException = new JSONObject();
        globalException.put("message",e.getMessage());
        globalException.put("type","notEnoughMoney");
        return globalException.toJSONString();
    }

}
